用户路由信息注册

BindUserHandler#bind()

1
2
3
4
//2.如果握手成功,就把用户链接信息注册到路由中心,本地和远程各一份
success = routerCenter.register(message.userId, message.getConnection());
//3.注册失败再处理下,防止本地注册成功,远程注册失败的情况,只有都成功了才叫成功
routerCenter.unRegister(message.userId, context.getClientType());

RouterCenter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//路由注册(本地、远程)
public boolean register(String userId, Connection connection) {
ClientLocation location = ClientLocation
.from(connection)
.setHost(mPushServer.getGatewayServerNode().getHost())
.setPort(mPushServer.getGatewayServerNode().getPort());

LocalRouter localRouter = new LocalRouter(connection);
RemoteRouter remoteRouter = new RemoteRouter(location);

LocalRouter oldLocalRouter = null;
RemoteRouter oldRemoteRouter = null;
try {
oldLocalRouter = localRouterManager.register(userId, localRouter);
oldRemoteRouter = remoteRouterManager.register(userId, remoteRouter);
} catch (Exception e) {
LOGGER.error("register router ex, userId={}, connection={}", userId, connection, e);
}
//如果本地路由已经存在,则通过事件,发送踢人消息给客户端,由客户端发出解绑消息unbind
if (oldLocalRouter != null) {
EventBus.post(new RouterChangeEvent(userId, oldLocalRouter));
LOGGER.info("register router success, find old local router={}, userId={}", oldLocalRouter, userId);
}
//如果远程路由已经存在,并且是在线状态,则通过事件,发送踢人消息给客户端,由客户端发出解绑消息unbind
if (oldRemoteRouter != null && oldRemoteRouter.isOnline()) {
EventBus.post(new RouterChangeEvent(userId, oldRemoteRouter));
LOGGER.info("register router success, find old remote router={}, userId={}", oldRemoteRouter, userId);
}
return true;
}
//删除路由(本地路由、远程路由)
public boolean unRegister(String userId, int clientType) {
localRouterManager.unRegister(userId, clientType);
remoteRouterManager.unRegister(userId, clientType);
return true;
}

RouterChangeListener.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Subscribe
@AllowConcurrentEvents
void on(RouterChangeEvent event) {
String userId = event.userId;
Router<?> r = event.router;
if (r.getRouteType().equals(Router.RouterType.LOCAL)) {
//发送踢人消息到客户端
sendKickUserMessage2Client(userId, (LocalRouter) r);
} else {
//广播踢人消息到消息中心(MQ)
//所有机器订阅MQ消息,如果当前机器不是目标机器,直接忽略,
//否则查询本地路由,找到要被踢下线的链接,发送踢人消息到客户端
sendKickUserMessage2MQ(userId, (RemoteRouter) r);
}
}

------ 本文结束 感谢您的阅读 ------